// pages/member/integral_transfer/integral_transfer.js
import {payKeyboardAsync} from "../../../mycomponent/payKeyboard/PayKeyboardAsync.js";
var app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
Page({
  behaviors: [skinBehavior],
  /**
	 * 页面的初始数据
	 */
  data: {
    integral: '积分',
    credit1: "0.00",
    transfer_id: '',
    memberInfo: '',
    info_form: {
      transfer_id: '',
      transfer_point: ''
    },

    //支付键盘
    payKeyboardShow:false,
    //是否需要支付密码
    need_password:false, 
    //是否设置支付密码
    has_password:false,  
    remark:'',
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    try {
      const value = wx.getStorageSync('integral');
      if (value) {
        this.setData({
          integral: value
        });
        // Do something with return value
        if (this.data.integral) {
          wx.setNavigationBarTitle({
            title: this.data.integral + '转账'
          });
        }
      }
    } catch (e) {
      // Do something when catch error
    }
   
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
    this._getIntegral();
  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  _getIntegral() {
    let that = this;
    let urlStr = app.getNetAddresss("finance.point-page.index");
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            credit1: res.data.credit1,
            need_password:res.data.need_password,
            has_password:res.data.has_password,
            rate:res.data.rate
          });
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: that.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  transferidchange() {
    var that = this;
    let urlStr = app.getNetAddresss("member.member.memberInfo");
    urlStr += '&uid=' + this.data.transfer_id;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            memberInfo: res.data
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  //受让人ID的值绑定
  transferidinp(e) {
    let val = e.detail.value;
    this.setData({
      transfer_id: val
    });
    this.transferidchange();
  },
  //转账的值绑定
  transferpointinp(e) {
    let val = e.detail.value;
    this.setData({
      'info_form.transfer_point': val
    });
  },
  remarkinp(e){
    let val = e.detail.value;
    this.setData({
      remark: val
    });
  },
  payKeyboardComplete(evt){
    payKeyboardAsync.complete(evt.detail);
  },
  getPayKeyboardPassword(){
    this.setData({payKeyboardShow:true});
    return new Promise((resove,reject)=>{
      payKeyboardAsync.addCompleteFn((pass)=>{
        resove(pass);
      });
    });
    // let pass = await this.getPayKeyboardPassword();
    // console.log(pass);
  },
  payKeyboardClose(){
    this.setData({payKeyboardShow:false});
  },
  //确认转账
  async confirm() {
    if (parseFloat(this.data.info_form.transfer_point) > parseFloat(this.data.credit1)) {
      wx.showToast({
        icon: 'none',
        title: `转让${this.data.integral}不可大于您的${this.data.integral}`,
        duration: 1500
      });
      return;
    }
    if (this.data.transfer_id == undefined || this.data.transfer_id <= 0 || this.data.transfer_id.length == 0) {
      wx.showToast({
        icon: 'none',
        title: '转让id不可为空',
        duration: 1500
      });
      return;
    }
    if (this.data.info_form.transfer_point == undefined || this.data.info_form.transfer_point <= 0 || this.data.info_form
      .length == 0) {
      wx.showToast({
        icon: 'none',
        title: `转让${this.data.integral}不可低于0`,
        duration: 1500
      });
      return;
    }

    if(this.data.need_password==true && this.data.has_password==false){
      let confirmFlag = await app.confirm("请先设置支付密码");
      if(confirmFlag) {
        wx.navigateTo({
          url: '/packageA/member/set_balance_password/set_balance_password'
        });
      }
      return;
    }

    let urlStr = app.getNetAddresss("finance.point-transfer.index");
    urlStr += '&recipient=' + this.data.transfer_id;
    urlStr += '&transfer_point=' + this.data.info_form.transfer_point;

    if(this.data.need_password==true){  //开启支付密码验证
      let pass = await this.getPayKeyboardPassword();
      urlStr += '&password=' + pass;
    }
    if(this.data.remark){
      urlStr += '&remark='+this.data.remark;
    }

    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500,
            success: function() {
              setTimeout(function() {
                //要延时执行的代码
                wx.navigateBack({
                  delta: 1
                });
              }, 1500); //延迟时间
            },
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  }
});
